<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="" prop="welfare">
        <el-input
          v-model="queryParams.welfare"
          placeholder="请输入福利奖品"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['configuration:perstoredrewards:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['configuration:perstoredrewards:remove']"
          >批量删除</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="perstoredrewardsList"
      @selection-change="handleSelectionChange"
      :span-method="objectSpanMethod"
      border
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="sort" />

      <el-table-column label="预存金额" align="center">
        <template slot-scope="scope">
          {{ scope.row.priceStart
          }}{{ scope.row.priceEnd === 0 ? "及以上" : "~" + scope.row.priceEnd }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="享受折扣" align="center" prop="discount" /> -->
      <el-table-column label="福利奖品" align="center" prop="welfare" />
      <el-table-column label="备注" align="center" prop="remarks">
        <template slot-scope="scope">
          <p style="white-space: pre-wrap; text-align: left">
            {{ scope.row.remarks }}
          </p>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['configuration:perstoredrewards:edit']"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['configuration:perstoredrewards:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预存奖励设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <div class="formGroup">
          <el-form-item
            label="预存金额"
            prop="priceStart"
            v-if="form.type == 1"
            class="formGroup-item1"
          >
            <el-input
              v-model="form.priceStart"
              placeholder="请输入金额"
              type="number"
            />
          </el-form-item>
          <el-form-item
            prop="priceEnd"
            v-if="form.type == 1"
            class="formGroup-item1"
            label-width="0"
          >
            <el-input
              v-model="form.priceEnd"
              placeholder="请输入金额"
              type="number"
            />
          </el-form-item>
          <el-form-item
            label="预存金额"
            v-if="form.type == 0"
            class="formGroup-item2"
          >
            <el-input
              v-model="form.priceStart"
              placeholder="请输入金额"
              type="number"
            />
          </el-form-item>
          <el-form-item prop="type" label-width="0">
            <el-radio-group v-model="form.type" inline>
              <el-radio-button label="0">以上</el-radio-button>
              <el-radio-button label="1">范围</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </div>

        <!-- <el-form-item label="折扣" prop="discount">
          <el-input v-model="form.discount" placeholder="请输入折扣" min="0" max="9.9" @input="handleInput">
            <template slot="append">折</template>
          </el-input>
        </el-form-item> -->
        <el-form-item label="福利奖品" prop="welfare">
          <el-input v-model="form.welfare" placeholder="请输入福利奖品" />
        </el-form-item>
        <el-form-item label="显示排序" prop="sort">
          <el-input
            v-model="form.sort"
            placeholder="请输入序号"
            type="number"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input
            type="textarea"
            v-model="form.remarks"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPerstoredrewards,
  getPerstoredrewards,
  delPerstoredrewards,
  addPerstoredrewards,
  updatePerstoredrewards,
} from "@/api/configuration/perstoredrewards";
import { accMul } from "@/utils/math";
export default {
  name: "Perstoredrewards",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预存奖励设置表格数据
      perstoredrewardsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        priceEnd: null,
        welfare: null,
      },
      // 表单参数
      form: { type: 1, discount: 100 },
      // 表单校验
      rules: {
        priceStart: [
          { required: true, message: "请输入金额", trigger: "blur" },
        ],
        priceEnd: [{ required: true, message: "请输入金额", trigger: "blur" }],
        type: [
          {
            required: true,
            message: "请选择类型 0  以上 范围 1",
            trigger: "change",
          },
        ],
        discount: [
          { required: true, message: "折扣不能为空", trigger: "blur" },
          {
            type: "number",
            validator: this.discountValidator,
            message: "请输入1-9.9之间的数字",
            trigger: "blur",
          },
        ],
        welfare: [
          { required: true, message: "福利奖品不能为空", trigger: "blur" },
        ],
        sort: [{ required: true, message: "序号不能为空", trigger: "blur" }],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    accMul,
    handleInput(event) {
      let newValue = event;
      // 移除非数字和小数点之外的字符
      newValue = newValue.replace(/[^\d.]/g, "");
      // 只保留第一个小数点
      newValue = newValue.replace(/^\./, "");
      newValue = newValue.replace(/\.(?=.*\.)/g, "");
      // 限制在0到9.9之间
      if (
        newValue !== "" &&
        (parseFloat(newValue) < 0 || parseFloat(newValue) > 9.9)
      ) {
        newValue = parseFloat(newValue) < 0 ? "0" : "9.9";
      }
      // 限制小数点后只能输入一位数字
      const decimalIndex = newValue.indexOf(".");
      if (decimalIndex !== -1 && newValue.length - decimalIndex > 2) {
        newValue = newValue.slice(0, decimalIndex + 2);
      }
      this.form.discount = newValue;
    },
    discountValidator(rule, value, callback) {
      if (value < 1 || value > 9.9) {
        return callback(new Error("输入折扣为1 - 9.9的数字"));
      }
      callback();
    },
    /** 查询预存奖励设置列表 */
    getList() {
      this.loading = true;
      listPerstoredrewards(this.queryParams).then((response) => {
        this.perstoredrewardsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 4) {
        if (rowIndex % this.perstoredrewardsList.length === 0) {
          return {
            rowspan: this.perstoredrewardsList.length,
            colspan: 1,
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0,
          };
        }
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        priceStart: null,
        priceEnd: null,
        type: 1,
        discount: 100,
        welfare: null,
        sort: null,
        remarks: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预存奖励设置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getPerstoredrewards(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预存奖励设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updatePerstoredrewards(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPerstoredrewards(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("确认要删除该预存奖励吗?")
        .then(function () {
          return delPerstoredrewards(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "configuration/perstoredrewards/export",
        {
          ...this.queryParams,
          ids: this.ids.length ? this.ids : undefined,
        },
        `perstoredrewards_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style>
.formGroup {
  display: flex;
  align-items: center;
  gap: 10px;
}

.formGroup-item1 .el-input {
  width: 110px;
}

.el-radio-group {
  width: 140px;
}
</style>
